package com.xyebank.ledger.service.xjfq.adaper;

import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.service.common.conveter.ApplyBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.DateUtil;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.vo.deal.XJFQCashApplyVo;

import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component(value = "XJFQApplyBeanConverter")
public class XJFQApplyBeanConverter implements ApplyBeanConverter<XJFQCashApplyVo> {

	ModelMapper modelMapper = new ModelMapper();

	// 不能采取注入方式
	private DateConverter dateConverter = new DateConverter();

	class NXApplyPropertyMap extends PropertyMap<XJFQCashApplyVo, LApplicationInf> {

		// 注意 不要有重复的映射，不然会报错
		protected void configure() {
			// 享宇订单号
			map(source.getOrderNo(), destination.getXyOrderNo());
			// 机构订单号
			map(source.getContactNo(), destination.getOrgOrderNo());
			// 电话号码
			map(source.getMobile(), destination.getMblNo());
			// 申请时间
			 using(dateConverter).map(source.getApplTime(), destination.getApplyTime());
			// 授信状态
//			map(source.getApprovalStatus(), destination.getStatus());
			// 授信金额（可用金额）
			map(source.getApprovalAmount(), destination.getCreditAmount());
			// 授信额度有效期
			map(source.getApprovalPeriod(), destination.getLoanTerm());
			// 产品利率
			map(source.getMonthlyRate(), destination.getInstRate());

		}
	}

	public XJFQApplyBeanConverter() {
		modelMapper.addMappings(new NXApplyPropertyMap());
	}

	@Override
	public LApplicationInf covert(XJFQCashApplyVo x) {
		LApplicationInf entity = modelMapper.map(x, LApplicationInf.class);
		entity.setDataSource(x.getChannelNo().equals("80008833") ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
		entity.setXyProductNo(ProductConstant.XJFQ_PROD_ID);
		entity.setXyOrgNo(ProductConstant.XJFQ_LOAN_ORG);
		entity.setId(IDUtil.generateId(x.getId().toString(), "xjfq_apply"));
		String status = x.getApprovalStatus();
		if("J".equals(status)){
			entity.setStatus(CommonConstant.APPLY_OVER);
		}else if ("R".equals(status) || "C".equals(status)) {
			entity.setStatus(CommonConstant.APPLY_FAIL);
		}else if ("N".equals(status) || "A".equals(status)) {
			entity.setStatus(CommonConstant.APPLY_SUCCESS);
		}else if ("U".equals(status)) {
			entity.setStatus(CommonConstant.APPLY_ING);
		}
		return entity;
	}
}
